﻿// *****************************************************************************
// **                   Ladenius MediaCenter TVSeries                         **
// ** ----------------------------------------------------------------------- **
// ** (c) 2008 by Jurjen Ladenius                                             **
// *****************************************************************************

#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ladenius.TheTVDB.Entities;
using Ladenius.TheTVDB;
using Effectory.Utilities;
#endregion

namespace Ladenius.Series.Configuration.WinClient.Forms {
    /// <summary>
    /// Form used for selecting a tv series from TheTVDB
    /// </summary>
    public partial class FrmTVDBSeries : Form {

        #region Fields
        private List<TheTVDB.Entities.Series> _foundSeries;
        private TheTVDBManager _manager;
        private object _lockObject = new object();
        private TheTVDB.Entities.Series _selectedSeries;
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="frmTVDBSeries"/> class.
        /// </summary>
        /// <param name="seriesName">Name of the series.</param>
        public FrmTVDBSeries(string seriesName, Language language) {
            InitializeComponent();
            _manager = new TheTVDBManager();
            this.Text = this.Text + " " + seriesName;
            txtSeries.Text = seriesName;
            btnOK.Enabled = false;
            AsyncExecution.BeginInvoke<string>(BeginFindSeries, EndFindSeries, seriesName);
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the selected series.
        /// </summary>
        /// <value>The selected series.</value>
        public TheTVDB.Entities.Series SelectedSeries {
            get { return _selectedSeries; }
        }
        #endregion

        #region Private methods
        private void BeginFindSeries(string seriesName) {
            lock (_lockObject) {
                _foundSeries = _manager.FindSeries(seriesName);
            }
        }

        private void EndFindSeries(IAsyncExecutionState state) {
            DisplaySeries();
        }
        #endregion

        #region Private methods - UI
        #region UI Delegates
        private delegate void UIMethod();
        #endregion


        /// <summary>
        /// Displays the series.
        /// </summary>
        private void DisplaySeries() {
            if (InvokeRequired) {
                BeginInvoke(new UIMethod(DisplaySeries));
                return;
            }

            lock (_lockObject) {
                lstSeries.Items.Clear();
                foreach (TheTVDB.Entities.Series series in _foundSeries) {
                    lstSeries.Items.Add(series.Name);
                }
            }

            DisplaySelection();
        }

        /// <summary>
        /// Displays the selection.
        /// </summary>
        private void DisplaySelection() {
            if (InvokeRequired) {
                BeginInvoke(new UIMethod(DisplaySelection));
                return;
            }

            lock (_lockObject) {
                int index = lstSeries.SelectedIndex;
                if (index >= 0) {
                    lblDescription.Text = _foundSeries[index].Overview;
                    btnOK.Enabled = true;
                } else {
                    lblDescription.Text = "Select a series to see it's description.";
                    btnOK.Enabled = false;
                }
            }
        }
        
        #endregion

        #region Events
        /// <summary>
        /// Handles the Click event of the btnFind control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnFind_Click(object sender, EventArgs e) {
            AsyncExecution.BeginInvoke<string>(BeginFindSeries, EndFindSeries, txtSeries.Text);
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the lstSeries control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void lstSeries_SelectedIndexChanged(object sender, EventArgs e) {
            DisplaySelection();
        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOK_Click(object sender, EventArgs e) {
            lock (_lockObject) {
                int index = lstSeries.SelectedIndex;
                if (index >= 0) {
                    _selectedSeries = _foundSeries[index];
                } else {
                    _selectedSeries = null;
                }
            }
            if (_selectedSeries != null) this.Close();
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e) {
            _selectedSeries = null;
            this.Close();
        }

        #endregion
    }
}
